<?php
namespace Umpmobile\Model;
use Think\Model;
class PromosionsModel extends Model{
	public $sid;
	public $cat_id;
	public $my_id;
	public $my_nick;
	public $friend_id;
	public $friend_info;
	public $cur_prom;
	public function getCurProm(){
		$data["sid"]=$this->sid;
		$proms = $this->where($data)->getField("`id`,`activity_id`, `act_cat_id`, `name`, `participate_range`, `start_time`, `end_time`, `is_user_tag`, `user_tag`, `user_tag_id`, `is_decrease_money`, `decrease_amount`, `friend_num`, `vip_user`, `sum_pay`, `pay_time_start`, `pay_time_end`, `item_iids`, `once_pay`, `status`, `promo_count`");
		$last = 0;
		foreach($proms as $k=>$prom){
			if($this->friend_info["friends"]>$last && $this->friend_info["friends"]<=$prom["friend_num"]){
				$prom["has_friends"]=$this->friend_info["friends"];
				$this->cur_prom = $prom;
				break;
			}
			$last = $prom["friend_num"];
		}
		return $this->cur_prom;
	}
	public function getInvProm(){
		// 读取被邀请人获得的折扣，仅限1个
		$data["sid"]=$this->sid;
		$prom = $this->where($data)->limit(1)->getField("`id`,`activity_id`, `act_cat_id`, `name`, `participate_range`, `start_time`, `end_time`, `is_user_tag`, `user_tag`, `user_tag_id`, `is_decrease_money`, `decrease_amount`, `is_discount`, `discount_rate`, `friend_num`, `vip_user`, `sum_pay`, `pay_time_start`, `pay_time_end`, `item_iids`, `once_pay`, `status`, `promo_count`");
		return $prom;
	}
	public function addUserTag(){
		Vendor("taobaosdk.top.TopClient");
		Vendor('taobaosdk.lotusphp_runtime.Logger.Logger');
		Vendor("taobaosdk.top.RequestCheckUtil");
		Vendor("taobaosdk.top.request.TmallPromotagTaguserSaveRequest");
		$SHOP = M("Shop");
		$sessionKey = $SHOP->where("`sid`=".$this->sid)->getField("sessionkey");
		
		$c = new \TopClient;
		$c->appkey = C("SM_KEY");
		$c->secretKey = C("SM_SECRET");
		$req = new \TmallPromotagTaguserSaveRequest;
		$req->setTagId($this->cur_prom["user_tag_id"]);
		$req->setNick($this->my_nick);
		$req->putOtherTextParam("top_mix_params", "nick");//就会进行解混淆
		$resp = $c->execute($req, $sessionKey);
		
		if($resp->is_success=="true"){
			$TLG = M("TagLog");
			$data["sid"]=$this->sid;
			$data["tag_id"]=$this->cur_prom["user_tag_id"];
			// $data["tag_name"]=$this->sid;
			$data["save_time"]=date("Y-m-d H:i:s");
			$data["mixednick"]=$this->my_nick;
			$data["friend_id"]=$this->friend_info["id"];
			$data["activity_id"]=$this->cur_prom["activity_id"];
			$data["prom_id"]=$this->cur_prom["id"];
			$data["act_cat_id"]=$this->cur_prom["act_cat_id"];
			$data["decrease_amount"]=$this->cur_prom["decrease_amount"];
			$data["friend_num"]=$this->friend_info["friends"];
			$TLG->add($data);
		}
		return $resp;
	}
}